<template>
	<el-card class="form-container">
		<el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()" label-width="90px">
			<el-form-item label="模版名称" prop="templateName">
				<el-input v-model="dataForm.templateName" placeholder="请输入模版名称"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button @click="cancel">取消</el-button>
				<el-button type="primary" @click="submit('dataForm')">确定</el-button>
			</el-form-item>
		</el-form>
	</el-card>
</template>

<script>
	import { addGoodsTemplate ,validataTemplateExist} from '@/api/mall/goodsTemplate'
	export default {
		name: 'add-goodsTemplate',
		data() {
			var validateTemplateName = (rule, value, callback) =>{
				console.log(value)
				validataTemplateExist(value).then(() =>{
					callback();
				}).catch((err) => {
					return callback(new Error(err.data.responseMessage));
				});
			};
			return {
				dataForm: {
					templateName: '',
				},
				dataRule: {
					templateName: [
						{ required: true, message: '请输入模版名称', trigger: 'blur'},
						{ validator: validateTemplateName, trigger: 'blur' }
					]
				}
			}
		},
		methods: {
			cancel() {
				this.$router.go(-1)
			},
			submit() {
				const set = this.$refs;
				set['dataForm'].validate((valid) => {
					if (!valid) {
						return false
					}
					addGoodsTemplate(this.dataForm).then(() => {
						this.$notify({
							title: '添加成功',
							message: '添加成功',
							type: 'success',
							duration: 2000,
							onClose: this.$router.go(-1)
						})
					})
				})
			}
		}
	}
</script>

<style>
</style>
